const app = getApp();
Component({
    properties: {},
    data: {
        humiEdge: 0,
        tempEdge: 0,
        distanceEdge: 0,
        fileList: [],
        token: '',
    },

    lifetimes: {
        attached() {
            this.setToken();
            if (this.data.token != '') {
                this.getAlertHumidity();
                this.getAlertTemperature();
            } else {
                wx.showToast({
                    title: '上方登录',
                    icon: 'none',
                    duration: 2000
                })
            }
        }
    },


    methods: {
        setToken() {
            this.setData({
                token: app.globalData.token
            })
            console.log("this.data.token")
        },


        getAlertHumidity() {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/3U0fClbRjIwu5c5QxVkP/attributes',
                method: 'GET',
                success: (res) => {
                    console.log(res)
                    this.setData({
                        humiEdge: res.data.client.alerthumidity,
                    })
                },
                fail: (res) => {
                    console.log(res)

                }
            })
        },
        getAlertTemperature() {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/3U0fClbRjIwu5c5QxVkP/attributes',
                method: 'GET',
                success: (res) => {
                    console.log(res)
                    this.setData({
                        tempEdge: res.data.client.alerttemperature,
                    })
                },
                fail: (res) => {
                    console.log(res)

                }
            })
        },

        postEdge(edge) {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/3U0fClbRjIwu5c5QxVkP/attributes',
                method: 'POST',
                data: {
                    "alerthumidity": edge,
                },
                success: (res) => {
                    console.log(res)
                },
                fail: (res) => {
                    console.log(res)
                }
            })
        },
        postEdge2(edge) {
            wx.request({
                url: 'http://43.143.141.18:8080/api/v1/3U0fClbRjIwu5c5QxVkP/attributes',
                method: 'POST',
                data: {
                    "alerttemperature": edge,
                },
                success: (res) => {
                    console.log(res)
                },
                fail: (res) => {
                    console.log(res)
                }
            })
        },

        onChange(event) {
            console.log(event.detail);
        },
        setHumiEdge(event) {
            this.setData({humiEdge: event.detail});
            this.postEdge(event.detail);
        },
        setTempEdge(event) {
            this.setData({tempEdge: event.detail});
            this.postEdge2(event.detail);
        },
        setDistanceEdge(event) {
            this.setData({distanceEdge: event.detail});
        }
    }
});
